<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/5/29
 * Time: 18:56
 */

namespace Admin\Controller;

use Admin\Controller\CommonController;


class UserController extends CommonController
{

   /*
    * 用户列表
    * */
   public function index()
   {
       $map = array();
       if (I('get.keyword') != '') {
           $keyword = I('get.keyword');
           $map['user_name|phone']=array('like','%'.$keyword.'%');
       }

       $list = D('User')->getList($map);
       $this->assign('count', $list['count']);
       $this->assign('totalPages', $list['totalPages']);// 总分页数
       $this->assign('nowPage', $list['nowPage']);// 当前页
       $this->assign('list', $list['list']);
       $this->assign('page', $list['show']);// 赋值分页输出
       $this->display();
   }

    /**
     * 添加用户
     */
    function addUser(){
        if(IS_POST){
            $msg=array('errno'=>1,'tip'=>'','url'=>'');
            $data['user_name'] = I('user_name');
            $data['phone'] = I('phone');
            $data['sex'] = I('sex');
            $data['age'] = I('age');
            $data['address'] = I('address');
            $data['user_card'] = I('user_card');
            $data['user_price'] = I('user_price');
            $data['reg_time'] =  time();

            $info = D('User')->where(array('phone'=>$data['phone']))->find();
            if($info) {
                $msg['error']='添加失败，该电话号码已经注册';
                $this->ajaxReturn($msg);
            }
            $id=D('User')->add($data);//添加用户
                if(!$id) {
                    $msg['error']='添加失败！';
                    $this->ajaxReturn($msg);
                }
                $msg['error'] = '添加成功';

          //充值管理
            if($data['user_price']>0){
                $map['user_id'] = $id;
                $map['price'] = $data['user_price'];
                $map['create_time'] = time();
                D('UserPrice')->add($map);//首次充值记录
            }
            $msg['errno']=0;
            $msg['url']=U('Admin/User/index');
            $this->ajaxReturn($msg);
        }
        $this->display();
    }
    /*
     * 用户充值记录列表
     * */
    public function price()
    {
        $map = array();
        if (I('get.keyword') != '') {
            $keyword = I('get.keyword');
            $map['user_name|phone']=array('like','%'.$keyword.'%');
        }

        $list = D('UserPrice')->getList($map);
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page', $list['show']);// 赋值分页输出
        $this->display();
    }
    /**
     * 用户充值
     */
    function addPrice(){
        if(IS_POST){
            $msg=array('errno'=>1,'tip'=>'','url'=>'');
            $user_id = I('user_id');
            $userInfo = D('User')->where(array('id'=>$user_id))->find();
            if(empty($userInfo)){
                $msg['error']='充值失败，该用户不存在';
                $this->ajaxReturn($msg);
            }
            $data['user_id'] = I('user_id');
            $data['price'] = I('price');
            $data['create_time'] =  time();

            $id=D('UserPrice')->add($data);//添加充值记录
            if(!$id) {
                $msg['error']='充值失败！';
                $this->ajaxReturn($msg);
            }
            $msg['error'] = '充值成功';

            D('User')->where(array('id'=>$data['user_id']))->setInc('user_price',$data['price']);//用户金额增加
            $msg['errno']=0;
            $msg['url']=U('Admin/User/price');
            $this->ajaxReturn($msg);
        }
        $user_id = I('user_id');
        $userInfo = D('User')->where(array('id'=>$user_id))->find();
        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /*
 * 用户开房记录列表
 * */
    public function room()
    {
        $map = array();
        if (I('get.keyword') != '') {
            $keyword = I('get.keyword');
            $map['user_name|phone']=array('like','%'.$keyword.'%');
        }

        $list = D('UserRoom')->getList($map);
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page', $list['show']);// 赋值分页输出
        $this->display();
    }
    /**
     * 添加开房记录
     */
    function addRoom(){
        if(IS_POST){
            $msg=array('errno'=>1,'tip'=>'','url'=>'');
            $user_id = I('user_id');
            $userInfo = D('User')->where(array('id'=>$user_id))->find();
            if(empty($userInfo)){
                $msg['error']='开房失败，该用户不存在';
                $this->ajaxReturn($msg);
            }
            if($userInfo['user_price']<I('room_price')){
                $msg['error']='开房失败，账户余额不足，请充值！';
                $this->ajaxReturn($msg);
            }
            $data['user_id'] = I('user_id');
            $data['room_name'] = I('room_name');
            $data['room_price'] = I('room_price');
            $data['user_point'] = I('user_point');
            $data['create_time'] =  time();

            $id=D('UserRoom')->add($data);//添加开房记录
            if(!$id) {
                $msg['error']='开房失败！';
                $this->ajaxReturn($msg);
            }
            //开房金额减少
            if($data['room_price']>0 ){
                $map['user_id'] =  $data['user_id'];
                $map['price'] = -$data['room_price'];
                $map['create_time'] = time();
                D('UserPrice')->add($map);//消费记录
            }
            //开房增加积分记录
            if($data['user_point']>0 ){
                $tel['user_id'] =  $data['user_id'];
                $tel['status'] = 1;
                $tel['point'] = $data['user_point'];
                $tel['create_time'] = time();
                D('UserPoint')->add($tel);//消费记录
            }
            D('User')->where(array('id'=>$data['user_id']))->setDec('user_price',$data['room_price']);//用户金额减少
            D('User')->where(array('id'=>$data['user_id']))->setInc('point',$data['user_point']);//用户积分增加
            $msg['error'] = '开房成功';
            $msg['errno']=0;
            $msg['url']=U('Admin/User/room');
            $this->ajaxReturn($msg);
        }
        $user_id = I('user_id');
        $userInfo = D('User')->where(array('id'=>$user_id))->find();
        $this->assign('userInfo',$userInfo);
        $this->display();
    }


    /*
   * 用户积分记录
   * */
    public function point()
    {
        $map = array();
        if (I('get.keyword') != '') {
            $keyword = I('get.keyword');
            $map['u.user_name|u.phone']=array('like','%'.$keyword.'%');
        }

        if(I('user_id')){
            $map['p.user_id'] = I('user_id');
        }
        $list = D('UserPoint')
            ->getList($map);
        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page', $list['show']);// 赋值分页输出
        $this->display();
    }

}